// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        touchPad:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._ellipseXR = 600
        this._ellipseYR = 200
        this._ellipseCenterX = 0
        this._ellipseCenterY = 140
        this._iconPositions = []
        let maxCorner = 120
        this.interval = maxCorner/4
        this._staticPoses = [-90-2*this.interval,-90-this.interval,-90,-90+this.interval,-90+2*this.interval]
        this.touchPad.on(cc.Node.EventType.TOUCH_START,this._onTableTouchBegin.bind(this),this);
        this.touchPad.on(cc.Node.EventType.TOUCH_MOVE,this._onTableTouchMove.bind(this),this);
        this.touchPad.on(cc.Node.EventType.TOUCH_END,this._onTableTouchEnd.bind(this),this);
        this.touchPad.on(cc.Node.EventType.TOUCH_CANCEL,this._onTableTouchCancel.bind(this),this);
        // this.testEllipse()
        this.initIconsPosiiton()

    },

    start () {

    },

    update (dt) {
        this._updateIconReturning(dt)
        this._updateIconScale(dt)
    },

    initLobbyTable(lobbyUIMgr){
        this.uiMgr = lobbyUIMgr
    },

    _updateIconScale(){
        let icons = this.touchPad.children

        for(let i=0;i<icons.length;i++){
            let node = icons[i]
            node.setScale(0.8 - ((node.y-(-46))/100 * 0.3))
        }
    },

    _updateIconReturning(dt){
        if(this._returning){
            if(Math.abs(this._waitReturnAngle) < 1){
                this.updateIconsPosition(this._waitReturnAngle)
                this._returning = false
            }else {
                if(this._waitReturnAngle > 0){
                    this.updateIconsPosition(1)
                    this._waitReturnAngle -= 1
                }else {
                    this.updateIconsPosition(-1)
                    this._waitReturnAngle += 1
                }
            }
        }
    },

    _onTableTouchBegin(event){
        this._returning = false
        let touchPos = event.getLocation()
        this._startPos = touchPos

        // let icons = this.touchPad.children
        // for(let i=0;i<icons.length;i++){
        //     let node = icons[i]
        //     let tmpBoundingBox = node.getBoundingBoxToWorld()
        //     if(tmpBoundingBox.contains(touchPos)){
        //         this._touchIcon = node
        //         break
        //     }
        // }
    },

    _onTableTouchMove(event){
        let delta = event.getDelta()

        // this.node.x += delta.x
        // this.node.y += delta.y
        this.updateIconsPosition(delta.x/8)
    },

    _onTableTouchEnd(event){
        this.iconReturnToStaticPositions()

        let touchPos = event.getLocation()
        let dis = this._startPos.sub(touchPos).mag();
        if(dis < 2){
            cc.log("手指移动距离小于10，按点击算")
            let icons = this.touchPad.children
            for(let i=0;i<icons.length;i++){
                let node = icons[i]
                let tmpBoundingBox = node.getBoundingBoxToWorld()
                if(tmpBoundingBox.contains(touchPos)){
                    let iconName = node.name
                    this.uiMgr.onEntranceClicked(iconName)
                }
            }
        }
    },

    _onTableTouchCancel(event){
        this.iconReturnToStaticPositions()

        let touchPos = event.getLocation()

        let icons = this.touchPad.children
        for(let i=0;i<icons.length;i++){
            let node = icons[i]
            let tmpBoundingBox = node.getBoundingBoxToWorld()
            if(tmpBoundingBox.contains(touchPos)){
                this._touchIcon = node
                break
            }
        }
    },

    _moveEllipse(angle){

    },

    _getPosInEllipse:function (corner) {

        corner *= 0.017453293
        // y=b*sinθ+y1=b/2+y1
        // x=a*cosθ+x1=a 3的平方根/2+x1
        let y = this._ellipseYR*Math.sin(corner)+this._ellipseCenterY
        let x = this._ellipseXR*Math.cos(corner)+this._ellipseCenterX
        return cc.v2(x,y)
    },

    initIconsPosiiton(){
        let icons = this.touchPad.children

        let maxCorner = 120
        let interval = maxCorner/4
        let poses = [-90-2*interval,-90-interval,-90,-90+interval,-90+2*interval]
        this._iconPositions = [].concat(poses)

        for(let i=0;i<poses.length;i++){
            let newP = icons[i]
            let p = this._getPosInEllipse(poses[i])
            // cc.log("big:",i,"  ",p)
            newP.position = p
        }
    },

    updateIconsPosition(addAngle){
        let icons = this.touchPad.children

        for(let i=0;i<this._iconPositions.length;i++){
            this._iconPositions[i] += addAngle
        }
        for(let i=0;i<this._iconPositions.length;i++){
            // cc.log("this._iconPositions[i]",this._iconPositions[i])
            if(this._iconPositions[i] <= -180){
                let maxAngle = Math.max(...this._iconPositions)
                this._iconPositions[i] = maxAngle+this.interval
                continue
            }
            if(this._iconPositions[i] >= 0){
                let minAngle = Math.min(...this._iconPositions)
                this._iconPositions[i] = minAngle-this.interval
                continue
            }
        }

        for(let i=0;i<this._iconPositions.length;i++){
            let newP = icons[i]
            let p = this._getPosInEllipse(this._iconPositions[i])
            newP.position = p
        }
    },

    iconReturnToStaticPositions(){
        this._returning = true
        let nowPoses = this._iconPositions
        let firstPos = nowPoses[0]

        let minDis = 9999
        let firstIdx = 0
        for(let i=0;i<this._staticPoses.length;i++){
            let dis = Math.abs(this._staticPoses[i] - firstPos)
            if(dis < minDis){
                minDis = dis
                firstIdx = i
            }
        }

        this._waitReturnAngle = this._staticPoses[firstIdx] - firstPos
    },

    async testEllipse () {
        let prefab = await cc.gg.resUtil.loadRes("test/p")
        for(let i=-180;i<0;i++){
            let newP = cc.instantiate(prefab)
            let p = this._getPosInEllipse(i)
            cc.log("p:",i,"  ",p)
            newP.position = p
            this.node.addChild(newP)
        }
        let big = await cc.gg.resUtil.loadRes("test/big")
        let poses = this._staticPoses

        for(let i=0;i<poses.length;i++){
            let newP = cc.instantiate(big)
            let p = this._getPosInEllipse(poses[i])
            cc.log("big:",i,"  ",p)
            newP.position = p
            this.node.addChild(newP)
        }
    }
});
